import portfolio_images as pi
from google.appengine.ext import db

class Model(db.Model):
    public_fields = []
    
    """Base class for models"""
    def fill(self, data):
        for field_name in self.public_fields:
            setattr(self, field_name, data.get(field_name, None))
            

class Feedback(Model):
    """User feedback message"""
    public_fields = ['author', 'content']
    
    author = db.StringProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
    user = db.UserProperty()
    
    @classmethod
    def get_top(cls, limit=100):
        return Feedback.all().order('-date').fetch(limit)


class PortfolioCategory(Model):
    """Portfolio category"""
    public_fields = ['title', 'name', 'position']
    
    title = db.StringProperty()
    name = db.StringProperty()
    position = db.IntegerProperty()
    
    
class PortfolioItem(Model):
    """Portfolio item"""
    public_fields = ['name', 'description', 'image', 'position']
    
    name = db.StringProperty()
    description = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
    position = db.IntegerProperty()
    
    """Fied for image raw data"""
    image = ''
    
    original_image = db.BlobProperty()
    full_size_image = db.BlobProperty()
    thumbnail_image = db.BlobProperty()
    
    category = db.ReferenceProperty(PortfolioCategory)
    
    def put(self, **kwargs):
        if self.image:
            self.original_image = db.Blob(self.image)
            self.image = ''
            self.full_size_image = db.Blob(pi.get_full_size_image(self.original_image))
            self.thumbnail_image = db.Blob(pi.get_thumbnail_image(self.original_image))
        return db.Model.put(self, **kwargs)
        
    def fill(self, data):
        if data.get('category_id', None):
            self.category = PortfolioCategory.get_by_id(int(data.get('category_id')))
        return Model.fill(self, data)